/**
 * Created by Student on 6/17/14.
 */
 //var app=angular.module
angular.module('mainCtrl',[])
            // inject the Comment service into our controller
    .controller('mainController', function($rootScope,$scope, $http, $modal, $log,simple,$routeParams) {
        // object to hold all the data for the new comment form
        $scope.simpleData = {}
        $scope.edit=false
        // loading variable to show the spinning loading icon
        $scope.loading = true;

        // get all the comments first and bind it to the $scope.comments object
        // use the function we created in our service
        // GET ALL COMMENTS ====================================================
        simple.get()
            .success(function(data) {
                $scope.simples = data;
                //console.log($scope.simples);
                $scope.loading = false;
            });

        // function to handle submitting the form
        // SAVE A COMMENT ======================================================
        $scope.store = function() {
            $scope.loading = true;

            simple.save($scope.simpleData)
                .success(function(data) {
                            //$scope.simpleData=data;
                            $scope.simpleData = {}
                            console.log($scope.simpleData);

                    // if successful, we'll need to refresh the comment list
                    simple.get()
                        .success(function(getData) {
                            $scope.simples = getData;
                            $scope.loading = false;
                        });

                })
                .error(function(data) {
                    console.log(data);
                });
     };

        // function to handle deleting a comment
        // DELETE A COMMENT ====================================================
        $scope.deleteSimple = function(id) {
            $scope.loading = true;
            // console.log(id);   
            // use the function we created in our service
            simple.destroy(id)
                .success(function(data) {

                    // if successful, we'll need to refresh the comment list
                    simple.get()
                        .success(function(getData) {
                            $scope.simples = getData;
                            $scope.loading = false;
                        });

                });
        };

        $scope.editSimple = function(id) {
            $scope.loading = true;
            $scope.edit=true;
             //console.log(id);   
            // use the function we created in our service
            simple.edit(id)
                .success(function(data) {
                        //console.log(data[0].name)
                         $scope.simpleData =data[0];
                            $scope.loading = false;
                        });

                };       
    $scope.editmain = function(id) {
            $scope.loading = true;
             $scope.edit=false;
             console.log(id);
            // use the function we created in our service
            simple.update(id,$scope.simpleData)
                .success(function(data) {
                            $scope.loading = false;
                        
                            // if successful, we'll need to refresh the comment list
                        simple.get()
                            .success(function(getData) {
                            $scope.simples = getData;
                            $scope.loading = false;
                        });
                    });

                };       

      $scope.detail=function(id){ 
        console.log(id);
        Comment.edit(id)
                        .success(function(data){            
                        $scope.simpleData=data
                        });
                    };
        $scope.open = function (id) {

            var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: ModalInstanceCtrl,
                resolve: {
                                                        
                     modalModel: function() {
                            return $scope.detail                     
                        }
              }

            })    
        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        })
    };    

});
var ModalInstanceCtrl=function ($scope, $modalInstance,modalModel) {

console.log(modalModel)
///$scope.commentData.author = $rootScope.commentData.author;
//$scope.commentData.text = $rootScope.commentData.text;    
    $scope.ok = function () {
        $modalInstance.close($scope.selected.item);
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
  };
    